﻿using UnityEngine;
using System.Collections;

public class PointlightSensor : LightSensor {
    
    public float Radius = 1.0f;

    // Use this for initialization
    void Start() {
        AddLight(this);
    }

    // Update is called once per frame
    void Update() {

    }

    public override bool IsInLight(Vector3 position, GameObject targetObject) {
        if (!light.enabled) {
            return false;
        }
        Vector3 originPoint = transform.position;
        Vector3 delta = position - originPoint;
        // distance test
        if (delta.sqrMagnitude > Radius * Radius) {
            return false;
        }
        
        // raycast test, return false if hit other object
        RaycastHit hit;
        if (Physics.Raycast(originPoint, delta, out hit, Radius)) {
            if (hit.collider.gameObject == targetObject) {
                return true;
            }
            return false;
        }
        return true;

    }
}
